package org.snowin.utils;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.snowin.common.SessionData;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ScreenCapture {
    String path;

    public ScreenCapture(SessionData sessionData) {
        try {
            sessionData.getLogger().info("~~~~~~~~~~~~ Start to Capture the Screen ~~~~~~~~~~~~");


            File srcFile = (File) ((TakesScreenshot) sessionData.getWebDriver()).getScreenshotAs(OutputType.FILE);
            String sp = GlobalInfo.getFileSeparator();
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");


            this.path = (sessionData.getLogFolder() + sp + sf.format(date).replace(":", "_") + ".jpg");


            sessionData.getLogger()
                    .info("~~~~~~~~~~~~ The Screen Capture is :" + this.path + " ~~~~~~~~~~~~");


            FileUtils.copyFile(srcFile, new File(this.path));
        } catch (IOException e) {
            sessionData.getLogger().info("######## [ Error ]: Screen Capture Failed !!!!!!!!!!!!!!!!!!!!!!###########");
            e.printStackTrace();
        }
        sessionData.getLogger().info("~~~~~~~~~~~~Finish to Capture the Screen ~~~~~~~~~~~~");
    }

    public String getPicPath() {
        return this.path;
    }
}
